package com.hskn.hss.module.attend.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.attend.entity.Attend;
import com.hskn.hss.module.attend.mapper.AttendMapper;
import com.hskn.hss.module.attend.service.IAttendService;
import com.hskn.hss.module.attend.vo.AttendanceSummaryVO;
import com.hskn.hss.module.attend.vo.CollectListVO;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.module.workruleitem.entity.WorkRuleItem;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Slf4j
@RestController
@RequestMapping("/hss/attend")
public class AttendController {

    @Autowired
    private IAttendService attendService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return AjaxResult.success(attendService.mList(params));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "collectList", method = RequestMethod.POST)
    public AjaxResult<Object> collectList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return AjaxResult.success(attendService.collectList(params));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.INSERT, tableName = "attend", bean = Attend.class, beanMapper = AttendMapper.class, names = "personName, gridName, planName, groupName")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult<Object> add(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            attendService.add(params);
            return AjaxResult.success("发布成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "attend", idKey = "attendId", bean = Attend.class, beanMapper = AttendMapper.class, names = "personName, gridName, planName, groupName")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            attendService.mUpdate(params);
            return AjaxResult.success("修改成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "出勤汇总导出接口", notes = "出勤汇总导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AttendanceSummaryVO.class)
    })
    @PostMapping("attendanceSummaryExcelExport")
    public com.hskn.hss.core.domain.AjaxResult attendanceSummaryExcelExport(HttpServletRequest request) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {

            JSONObject params = JSON.parseObject(Tools.getBody(request));

            CollectListVO collectListVO = attendService.collectList(params);

            List<Map<String, Object>> list = collectListVO.getList();

            List<AttendanceSummaryVO> attendanceSummaryVOList = new ArrayList<>();

            list.forEach(listMap -> {
                AttendanceSummaryVO attendanceSummaryVO = new AttendanceSummaryVO();
                attendanceSummaryVO.setWorkEmployeeName(listMap.get("personName").toString());
                attendanceSummaryVO.setDepName(listMap.get("depName").toString());
                attendanceSummaryVO.setGridName(listMap.get("gridName").toString());
                attendanceSummaryVO.setPlanName(listMap.get("planName").toString());
                attendanceSummaryVO.setShouldAttendanceDays(listMap.get("ycqts").toString());
                attendanceSummaryVO.setActualAttendanceDays(listMap.get("cqts").toString());
                attendanceSummaryVO.setLateNumber(listMap.get("cdcs").toString());
                attendanceSummaryVO.setLeaveNumber(listMap.get("ztcs").toString());
                attendanceSummaryVO.setAbsenteeismNumber(listMap.get("kgcs").toString());
                attendanceSummaryVOList.add(attendanceSummaryVO);
            });
            //工具类实例化
            ExcelUtil<AttendanceSummaryVO> util = new ExcelUtil<>(AttendanceSummaryVO.class);
            //导出
            return util.exportExcel(attendanceSummaryVOList, "AttendanceSummary");
        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }
}

